<?php
namespace app\hesuan\controller;
use think\Controller;
use think\Request;
use think\Db;
use DateTime;
class Admin extends Controller
{
    public function index()
    {

        if(session('admin_num')==null){
            return "请先登录！";
        }

        $init_date = "2022-04-18"; 
        $today = date("Y-m-d H:i:s");

        $datetime_init = new DateTime($init_date);
        $datetime_today = new DateTime($today);
        $days = $datetime_init->diff($datetime_today)->days;
        $week_num = floor($days / 7) +1  ; //当前日期处于第几周
        $day_delta1 = $days % 7; //相对于本周一偏离的日期
        $day_delta2 = 6 - $day_delta1; //相对于本周日偏离的日期
        $day_delta3 = $day_delta1+7; //相对于上周一偏离的日期
        $day_delta4 = $day_delta1+1; //相对于上周日偏离的日期
        $day_delta5 = $day_delta3+7; //相对于上上周一偏离的日期

        $thisweek_start = date("Y-m-d", strtotime("-".$day_delta1." day"));
        $thisweek_end = date("Y-m-d", strtotime($day_delta2." day"));
        $lastweek_start = date("Y-m-d", strtotime("-".$day_delta3." day"));
        $lastweek_end = date("Y-m-d", strtotime("-".$day_delta4." day"));
        $lastlastweek_start = date("Y-m-d", strtotime("-".$day_delta5." day"));
        $deadline = new DateTime($thisweek_end);

        // if($datetime_today < $deadline){
        //     return "系统未开放！请在周日12:00后重试。";
        // }

        $where = [
            'created_at'=>['between',[$thisweek_start." 00:00:00", $thisweek_end." 23:59:59"]]

        ];
        
        $this->assign('record_number',''.Db::table('hesuan_info')->where($where)->count());
        $this->assign('thisweek_start', $thisweek_start);
        $this->assign('thisweek_end', $thisweek_end);
        return $this->fetch();
    }



    public function export_all()
    {

        if(session('admin_num')==null){
            return "请先登录！";
        }


        // 导出全部可以借阅的图书数据到Excel表格

        //查询需要导出的数据集合
        // $map['u.is_del'] = 0;
        $init_date = "2022-04-18"; 
        $today = date("Y-m-d H:i:s");
        $datetime_init = new DateTime($init_date);
        $datetime_today = new DateTime($today);
        $days = $datetime_init->diff($datetime_today)->days;
        $week_num = floor($days / 7) +1  ; //当前日期处于第几周
        $day_delta1 = $days % 7; //相对于本周一偏离的日期
        $day_delta2 = 6 - $day_delta1; //相对于本周日偏离的日期
        $thisweek_start = date("Y-m-d", strtotime("-".$day_delta1." day"));
        $thisweek_end = date("Y-m-d", strtotime($day_delta2." day"));


        $where = [
            'created_at'=>['between',[$thisweek_start." 00:00:00", $thisweek_end." 23:59:59"]]

        ];
        $xlsData = Db::table('hesuan_info')
        ->where($where)
            // ->alias("a")//取一个别名
            // ->join('user_info u', 'a.user_id = u.id', 'LEFT')
            // //想要的字段
            // ->field('u.id as user_id,u.num,u.name,u.sex,u.tel,u.email,u.is_del,a.id as apply_id,
            //     a.graduated_from_school,a.direction,a.id_num,
            //     a.file_remarks')
            // ->where($map)
            // ->order('a.file_1_vetified,a.file_2_vetified,u.num')
            ->select();
        // echo "haha";
        // print(count($xlsData));

        //这里引入PHPExcel文件
        vendor("PHPExcel");
        vendor("PHPExcel.Writer.Excel5");
        vendor("PHPExcel.Writer.Excel2007");
        vendor("PHPExcel.IOFactory");
        $objExcel = new \PHPExcel();

        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
        $objActSheet = $objExcel->getActiveSheet();

        //设置Excel表格各列标题
        $letter = explode(',', "A,B,C,D,E,F,G,H,I,J,K,L,M,N");
        $arrHeader = array('编号', '学号', '姓名', '本周是否已做核酸', '已做核酸地点','未做核酸原因', '近期出入校状态', '录入时间', '是否为本人本周最新填报');
        $lenth = count($arrHeader);
        for ($i = 0; $i < $lenth; $i++) {
            $objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
        };

        $a = 0; //序号
        //填充相应的数据到Excel表格中
        foreach ($xlsData as $k => $v) {
            $k += 2; //需要明白为什么加2,因为这个k代表$xlsData的索引,后面需要用它来表示应该存到Excel的第几行
            $a += 1;
            $objActSheet->setCellValue('A' . $k, $a);//
            // $objActSheet->setCellValue('B' . $k, $v['num']);
            // $objActSheet->setCellValue('C' . $k, $v['name']);
            // $objActSheet->setCellValue('D' . $k, $v['tel']);
            // $objActSheet->setCellValue('E' . $k, $v['email']);
            $objActSheet->setCellValue('B' . $k, $v['num']);
            $objActSheet->setCellValue('C' . $k, $v['name']);
            $objActSheet->setCellValue('D' . $k, $v['is_tested']);
            $objActSheet->setCellValue('E' . $k, $v['place']);
            $objActSheet->setCellValue('F' . $k, $v['reason']);
            $objActSheet->setCellValue('G' . $k, $v['status']);
            $objActSheet->setCellValue('H' . $k, $v['created_at']);
            $objActSheet->setCellValue('I' . $k, $v['is_newest']);

            // Z 列后是 AA , AB , 如下所示
            //$objActSheet->setCellValue('AA' . $k, $v['type']);
            //$objActSheet->setCellValue('AB' . $k, $v['type']);

            $objActSheet->getRowDimension($k)->setRowHeight(20); //
        }

        //设置表格各列的宽度
        $objActSheet->getColumnDimension('A')->setWidth(8);
        $objActSheet->getColumnDimension('B')->setWidth(20);
        $objActSheet->getColumnDimension('C')->setWidth(10);
        $objActSheet->getColumnDimension('D')->setWidth(20);
        $objActSheet->getColumnDimension('E')->setWidth(25);
        $objActSheet->getColumnDimension('F')->setWidth(25);
        $objActSheet->getColumnDimension('G')->setWidth(20);
        $objActSheet->getColumnDimension('H')->setWidth(25);
        $objActSheet->getColumnDimension('I')->setWidth(25);
        // $objActSheet->getColumnDimension('N')->setWidth(30);

        //设置输出文件信息
        date_default_timezone_set('PRC');
        $today = date('yymd_His');
        $outfile = "本周核酸统计表_" . $today . ".xlsx";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $outfile . '"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');    //这里直接导出文件
    }



    




    public function export_none()
    {

        if(session('admin_num')==null){
            return "请先登录！";
        }

        
        // 导出全部可以借阅的图书数据到Excel表格

        //查询需要导出的数据集合
        // $map['u.is_del'] = 0;
        $init_date = "2022-04-18"; 
        $today = date("Y-m-d H:i:s");
        $datetime_init = new DateTime($init_date);
        $datetime_today = new DateTime($today);
        $days = $datetime_init->diff($datetime_today)->days;
        $week_num = floor($days / 7) +1  ; //当前日期处于第几周
        $day_delta1 = $days % 7; //相对于本周一偏离的日期
        $day_delta2 = 6 - $day_delta1; //相对于本周日偏离的日期
        $thisweek_start = date("Y-m-d", strtotime("-".$day_delta1." day"));
        $thisweek_end = date("Y-m-d", strtotime($day_delta2." day"));


        $where = [
            'created_at'=>['between',[$thisweek_start." 00:00:00", $thisweek_end." 23:59:59"]]
        ];
        
        $subQuery = Db::table('hesuan_info')->field('num')->where($where)->select(false);
        

        $xlsData = Db::query("select * from student_info where num not in (".$subQuery.")");


        // var_dump($xlsData);
        // 这里引入PHPExcel文件
        vendor("PHPExcel");
        vendor("PHPExcel.Writer.Excel5");
        vendor("PHPExcel.Writer.Excel2007");
        vendor("PHPExcel.IOFactory");
        $objExcel = new \PHPExcel();

        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
        $objActSheet = $objExcel->getActiveSheet();

        //设置Excel表格各列标题
        $letter = explode(',', "A,B,C,D,E,F,G,H,I,J,K,L,M,N");
        $arrHeader = array('编号', '学号', '姓名', '性别', '电话','学苑');
        $lenth = count($arrHeader);
        for ($i = 0; $i < $lenth; $i++) {
            $objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
        };

        $a = 0; //序号
        //填充相应的数据到Excel表格中
        foreach ($xlsData as $k => $v) {
            $k += 2; //需要明白为什么加2,因为这个k代表$xlsData的索引,后面需要用它来表示应该存到Excel的第几行
            $a += 1;
            $objActSheet->setCellValue('A' . $k, $a);//
            $objActSheet->setCellValue('B' . $k, $v['num']);
            $objActSheet->setCellValue('C' . $k, $v['name']);
            $objActSheet->setCellValue('D' . $k, $v['sex']);
            $objActSheet->setCellValue('E' . $k, $v['tel']);
            $objActSheet->setCellValue('F' . $k, $v['class']);

            // Z 列后是 AA , AB , 如下所示
            //$objActSheet->setCellValue('AA' . $k, $v['type']);
            //$objActSheet->setCellValue('AB' . $k, $v['type']);

            $objActSheet->getRowDimension($k)->setRowHeight(20); //
        }

        //设置表格各列的宽度
        $objActSheet->getColumnDimension('A')->setWidth(8);
        $objActSheet->getColumnDimension('B')->setWidth(20);
        $objActSheet->getColumnDimension('C')->setWidth(10);
        $objActSheet->getColumnDimension('D')->setWidth(20);
        $objActSheet->getColumnDimension('E')->setWidth(25);
        $objActSheet->getColumnDimension('F')->setWidth(25);
        // $objActSheet->getColumnDimension('N')->setWidth(30);

        //设置输出文件信息
        date_default_timezone_set('PRC');
        $today = date('yymd_His');
        $outfile = "本周未填写核酸_" . $today . ".xlsx";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $outfile . '"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');    //这里直接导出文件
    }









    public function login()
    {
        return $this->fetch();
    }   

    public function dologin(){
        if($this->request->isPost()){

            $username = $this->request->post('username/s','','trim');
            $password = $this->request->post('password/s','','trim');
            $count = Db::table('admin_info')->where('num',$username)->where('tel',$password)->count();
            // echo $count;
            // var_dump( Db::table('student_info')->where('tel','=',$password)->select());
            // return 'haha';
            if($count!=1){
                return '用户名或密码错误！';
            }
            else{
                session('admin_num',$username);
                session('admin_name',Db::table('admin_info')->where('num',$username)->column('name')[0]);
                return $this->fetch();
            }
        }
    }

    public function logout(){
        session(null);
        return "退出登录";
    }



    
}
